package com.decoration.basic.controller;

import com.decoration.basic.BasicController;
import com.decoration.basic.entity.DecPhaseInf;
import com.decoration.basic.entity.DecorationUser;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.findCondition.DecPhaseFindCondition;
import com.decoration.basic.service.DecPhaseService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.Global;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CtctionPhaseController extends BasicController {

	@Resource
	private DecPhaseService decPhaseService;

	@Resource
	private DecUserService decUserService;

	@RequestMapping(value = "/phase/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addDecPhase(JsonResult jsonResult, @RequestBody DecPhaseInf decPhaseInf) {
		if (null == decPhaseInf || "".equals(decPhaseInf)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要添加的项目阶段信息为空,请重试!");
			return jsonResult;
		}

		int res = decPhaseService.insert(decPhaseInf);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目阶段添加失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("添加成功");
		return jsonResult;
	}

	//更新阶段-监理申请，必须是1状态，申请结束改为2，业主审批，必须是2状态，审批结束改为3，管理审批，必须是3状态，审批结束改为4
	@RequestMapping(value = "/phaseExam/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateCarousel(JsonResult jsonResult, HttpServletRequest request, @RequestBody DecPhaseInf decPhaseInf) {
        //校验用户信息
		String userid = (String) request.getSession().getAttribute(Global.SESSION_USER_ID);
		/*if (userid == null || userid.equals("")){
			userid = "a4f9f6da4551419cbdb77ffaec1c55e9";
		}*/
		DecorationUser user = decUserService.findById(userid);
		if (user ==null){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("用户未登录，请联系管理员");
			return jsonResult;
		}

		//校验阶段情况
		DecPhaseInf phaseInf = decPhaseService.findPhaseById(decPhaseInf.getPhaseId());
		if (null == phaseInf) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要更新的项目阶段账号信息为空,请重试!");
			return jsonResult;
		}
		//校验输入参数与数据库参数是否一直，阶段顺序及阶段状态
		if (phaseInf.getPhaseRank() == decPhaseInf.getPhaseRank() && phaseInf.getVaildFlag() == decPhaseInf.getVaildFlag()){
			decPhaseInf.setVaildFlag(decPhaseInf.getPhaseRank()+1);
		}
		//管理审批3状态
		if (user.getPerType()==0 && decPhaseInf.getVaildFlag()==3){
			DecPhaseInf nextPhaseInf = new DecPhaseInf();
			nextPhaseInf.setCtcId(phaseInf.getCtcId());
			nextPhaseInf.setPhaseRank(phaseInf.getPhaseRank());
			//更新下一个阶段为进度状态为1
			decPhaseService.updateNextPhase(decPhaseInf);
		}else if(user.getPerType()==3 && decPhaseInf.getVaildFlag()==2){//业主审批2状态，业主归属于该项目

		}else if(user.getPerType()==2 && decPhaseInf.getVaildFlag()==1){//监理提交1状态,监理归属于该项目

		}else {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("该用户无权限申请审批流程,请联系管理员!");
			return jsonResult;
		}

		int res = decPhaseService.updateById(decPhaseInf);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目阶段更新失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("更新成功");
		return jsonResult;
	}

	@RequestMapping(value = "/phase/del", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delCarousel(JsonResult jsonResult, @RequestBody DecPhaseInf decPhaseInf) {
		if (null == decPhaseInf.getPhaseId() || "".equals(decPhaseInf.getPhaseId())) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要删除的项目阶段账号信息为空,请重试!");
			return jsonResult;
		}
		int res = decPhaseService.deleteById(Integer.parseInt(String.valueOf(decPhaseInf.getPhaseId())));
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目阶段删除失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("删除成功");
		return jsonResult;
	}

	//根据项目id获取全部阶段信息
	@RequestMapping(value = "/phaseList/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getCarouselList(JsonResult jsonResult, @RequestBody DecPhaseFindCondition findCondition) {
		//setPagerInfo(findCondition);
		List<DecPhaseInf> list = decPhaseService.findAll(findCondition);
//		Integer count = decPhaseService.getDecPhaseInfSize();
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(list.size());
		jsonResult.setData(list);
		return jsonResult;
	}

	@RequestMapping(value = "/phase/{phaseid}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getCarousel(JsonResult jsonResult, @PathVariable String phaseId) {
		DecPhaseInf decPhaseInf = decPhaseService.findPhaseById(phaseId);
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setData(decPhaseInf);
		return jsonResult;
	}
}
